package com.itheima.reggie.contorller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加购物车:{}", shoppingCart);
        Long user = BaseContext.getCurrentId();//当前登陆用户
        //1. 判断购物车中是否存在该菜品或者套餐
        //查询条件：根据当前登陆用户，菜品id或者套餐id
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();;
        lqw.eq(ShoppingCart::getUserId, user);
        //如果菜品id不为空，说明当前用户添加的是一个菜品，根据菜品id查询购物车
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //如果套餐id不为空，说明当前用户添加的是一个套餐，根据套餐id查询购物车
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCartDB = shoppingCartService.getOne(lqw);

        if(shoppingCartDB != null) {
            //2. 如果存在，数量+1 update
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartDB);
            return R.success(shoppingCartDB);
        } else{
            //3. 如果不存在，添加购物车 数量默认1 insert
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(user);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }


    /**
     *  购物车列表查询接口，先提供一个空方法，防止前台页面报错
     * @return
     */
    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //根据当前登陆用户查询购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //根据创建时间升序排序
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> cartList = shoppingCartService.list(lqw);
        return R.success(cartList);
    }

    @DeleteMapping("clean")
    public R clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //根据当前登陆用户查询购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");

    }
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        ShoppingCart cart = shoppingCartService.sub(shoppingCart);
        return R.success(cart);
    }
}